package top.chmin.project.cloud.common.entity.auto;

// field import auto
// #bind.field.tableCode#

import cn.hutool.core.bean.BeanUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.chmin.project.cloud.common.entity.Page;

import java.util.List;
import java.util.stream.Stream;



/**
 * chmin
 * 2023-12-27 14:43:41
 * cloud-builder
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class EntityService {

    @Resource
    EntityPlugin plugin;

    @Resource
    EntityDao entityDao;

// field labelDao auto

    public int save(EntitySave.Param param) {
        plugin.checkSave(param);
        Entity entity = BeanUtil.toBean(param, Entity.class);
        plugin.beforeSave(entity);
        int result = entityDao.save(entity);
        plugin.afterSave(param, entity, result);
        return result;
    }

    public int updateById(EntityUpdateById.Param param) {
        plugin.checkUpdateById(param);
        Entity entity = BeanUtil.toBean(param, Entity.class);
        plugin.beforeUpdateById(entity);
        int result = entityDao.updateById(entity);
        plugin.afterUpdateById(param, entity, result);
        return result;
    }

    public int deleteById(EntityDeleteById.Param param) {
        plugin.checkDeleteById(param);
        int result = entityDao.deleteById(param.getId());
        plugin.afterDeleteById(param, result);
        return result;
    }

    public EntitySelectById.Result selectById(EntitySelectById.Param param) {
        plugin.checkSelectById(param);
        EntityFPC fpc = new EntityFPC().id().eq(param.getId());
        plugin.beforeSelectById(param, fpc);
        EntitySelectById.Result result = entityDao.getFirst(fpc.toCondition(), EntitySelectById.Result.class);
        plugin.afterSelectById(param, result);
        return result;
    }

    public Page<EntitySelectPage.Result> selectPage(EntitySelectPage.Param param) {
        plugin.checkSelectPage(param);
        EntityFPC fpc = new EntityFPC();
        fpc = param.isDesc() ? fpc.id().desc() : fpc.id().asc();
        plugin.beforeSelectPage(param, fpc);
        Page<EntitySelectPage.Result> result = entityDao.getPageByCondition(fpc.toCondition(), EntitySelectPage.Result.class);
// field label auto
        plugin.afterSelectPage(param, result);
        return result;
    }

    // entity select auto
    public List<EntityTableCodeSelect.Result> selectTableCode(EntityTableCodeSelect.Param param) {
        plugin.checkSelectTableCode(param);
        EntityFPC fpc = new EntityFPC();
        plugin.beforeSelectTableCode(param, fpc);
        Stream<EntityTableCodeSelect.Result> result = entityDao.getStreamByCondition(fpc.toCondition(), EntityTableCodeSelect.Result.class)
                .peek(r -> {
                    r.setValue(r.getTableCode());
                    r.setLabel(r.getTableName());
                });
        List<EntityTableCodeSelect.Result> list = result.toList();
        plugin.afterSelectTableCode(param, list);
        return list;
    }


// entity search auto

}
